package com.ubing.boot.notesforjava.test.juc;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : volatile_test
 * @Description : volatile_test
 * @Author : Xxxxx
 * @Date: 2023-03-14 10:07
 */
public class volatile_test {
    volatile List lists=new ArrayList();
//    volatile List lists= Collections.synchronizedList(new ArrayList());
    public void add(Object o){
        lists.add(o);
    }
    public int size(){
        return lists.size();
    }

    public static void main(String[] args) throws InterruptedException {
        volatile_test volatile_test = new volatile_test();
//        for (int i = 0; i <5 ; i++) {
           Thread t1= new Thread(()->{
                while (true){
                    if(volatile_test.size()==5){
                        break;
                    }
                }
                System.out.println(Thread.currentThread().getName()+"t"+"  over");
            },"监视线程");
           t1.start();
//        }
//        new Thread(()->{
//            while (true){
//                if(volatile_test.size()==5){
//                    break;
//                }
//            }
//            System.out.println("t2  over");
//        }).start();

        Thread t2 =new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName()+"add"+i);
                volatile_test.add(new Object());
//                try {
//                    TimeUnit.SECONDS.sleep(1);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        },"操作线程");
        t2.start();
        t1.join();
        t2.join();
    }
}
